package com.bsj.pay.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bsj.pay.dao.BalanceDao;
import com.bsj.pay.dao.ChargeAccountDao;
import com.bsj.pay.dao.PartnerDao;
import com.bsj.pay.model.BalanceBean;
import com.bsj.pay.model.ChargeAccountBean;
import com.bsj.pay.model.ChargeAccountBean;
import com.bsj.pay.model.PartnerBean;
import com.bsj.pay.service.PartnerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class PartnerServiceImpl implements PartnerService{
	
	@Autowired
	private PartnerDao dao;
	
	@Autowired
	private BalanceDao balanceDao;
	
	@Autowired
	private ChargeAccountDao chargeAccountDao;

	@Override
	public long insert(PartnerBean t) {
		// TODO Auto-generated method stub
		return dao.insert(t);
	}

	@Override
	public int deleteById(Long id) {
		// TODO Auto-generated method stub
		return dao.deleteById(id);
	}

	@Override
	public List<PartnerBean> queryList(PartnerBean t) {
		// TODO Auto-generated method stub
		return dao.queryList(t);
	}

	@Override
	public int update(PartnerBean t) {
		// TODO Auto-generated method stub
		return dao.update(t);
	}

	@Override
	public PartnerBean getById(Long Id) {
		// TODO Auto-generated method stub
		return dao.getById(Id);
	}

	@Override
	public long count(PartnerBean t) {
		// TODO Auto-generated method stub
		return dao.count(t);
	}

	@Override
	public PageInfo<PartnerBean> pageList(PartnerBean t, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		List<PartnerBean> list = dao.queryList(t);
		PageInfo<PartnerBean> pageInfo= new PageInfo<PartnerBean>(list);
		return pageInfo;
	}

	@Override
	public int removeById(Long id) {
		// TODO Auto-generated method stub
		return dao.removeById(id);
	}

	@Override
	public List<PartnerBean> queryAll() {
		// TODO Auto-generated method stub
		return dao.queryAll();
	}

	@Override
	public synchronized int addQuota(long id, int amount,String remarks) {
		// TODO Auto-generated method stub
		PartnerBean partner=dao.getById(id);
		BalanceBean insertModel=new BalanceBean();
		insertModel.setUser_id(partner.getId());
		insertModel.setType(2);
		insertModel.setCreate_time(System.currentTimeMillis()/1000);
		insertModel.setAmount(amount);
		insertModel.setAfter_balance(partner.getQuota()+amount);
		insertModel.setRemarks(remarks);
		balanceDao.insert(insertModel);
		return dao.addQuota(id, amount);
	}

	@Override
	public synchronized int subQuota(long id, int amount,String remarks) {
		// TODO Auto-generated method stub
		PartnerBean partner=dao.getById(id);
		BalanceBean insertModel=new BalanceBean();
		insertModel.setUser_id(partner.getId());
		insertModel.setType(2);
		insertModel.setCreate_time(System.currentTimeMillis()/1000);
		insertModel.setAmount(-amount);
		insertModel.setAfter_balance(partner.getQuota()-amount);
		insertModel.setRemarks(remarks);
		balanceDao.insert(insertModel);
		return dao.subQuota(id, amount);
	}

	@Override
	public synchronized int addQuotaByChargePhoneId(long id, int amount,String remarks) {
		// TODO Auto-generated method stub
		ChargeAccountBean chargePhone=chargeAccountDao.getById(id);
		PartnerBean partner=dao.getById(chargePhone.getPartner_id());
		BalanceBean insertModel=new BalanceBean();
		insertModel.setUser_id(partner.getId());
		insertModel.setType(2);
		insertModel.setCreate_time(System.currentTimeMillis()/1000);
		insertModel.setAmount(amount);
		insertModel.setAfter_balance(partner.getQuota()+amount);
		insertModel.setRemarks(remarks);
		balanceDao.insert(insertModel);
		return dao.addQuota(chargePhone.getPartner_id(), amount);
	}

	@Override
	public synchronized int subQuotaByChargePhoneId(long id, int amount,String remarks) {
		// TODO Auto-generated method stub
		ChargeAccountBean chargePhone=chargeAccountDao.getById(id);
		PartnerBean partner=dao.getById(chargePhone.getPartner_id());
		BalanceBean insertModel=new BalanceBean();
		insertModel.setUser_id(partner.getId());
		insertModel.setType(2);
		insertModel.setCreate_time(System.currentTimeMillis()/1000);
		insertModel.setAmount(-amount);
		insertModel.setAfter_balance(partner.getQuota()-amount);
		insertModel.setRemarks(remarks);
		balanceDao.insert(insertModel);
		return dao.subQuota(chargePhone.getPartner_id(), amount);
	}

}
